package com.canaan.business.service.print.impl;

import java.util.Date;
import java.util.List;

import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.print.PrintRecordMapper;
import com.canaan.business.domain.print.PrintRecord;
import com.canaan.business.service.print.IPrintRecordService;
import com.canaan.common.annotation.TableName;
/**
 * 打印记录Service业务层处理
 *
 * @author canaan
 * @date 2025-04-20
 */
@Service
public class PrintRecordServiceImpl implements IPrintRecordService {
    @Resource
    private PrintRecordMapper printRecordMapper;

    /**
     * 查询打印记录
     *
     * @param id 打印记录主键
     * @return 打印记录
     */
    @Override
    public PrintRecord selectPrintRecordById(Long id) {
        return printRecordMapper.selectPrintRecordById(id);
    }

    /**
     * 查询打印记录列表
     *
     * @param printRecord 打印记录
     * @return 打印记录
     */
    @Override
    public List<PrintRecord> selectPrintRecordList(PrintRecord printRecord) {
        return printRecordMapper.selectPrintRecordList(printRecord);
    }

    /**
     * 新增打印记录
     *
     * @param printRecord 打印记录
     * @return 结果
     */
    @Override
    public int insertPrintRecord(PrintRecord printRecord) {
        printRecord.setCreateBy(SecurityUtils.getUsername());
        printRecord.setCreateTime(DateUtils.getNowDate());
        return printRecordMapper.insertPrintRecord(printRecord);
    }

    /**
     * 批量新增打印记录
     *
     * @param printRecordList 打印记录列表
     * @return 结果
     */
    @Override
    public int batchInsertPrintRecord(List<PrintRecord> printRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (PrintRecord printRecord : printRecordList){
            printRecord.setCreateBy(username);
            printRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<printRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > printRecordList.size()) {
                endIndex = printRecordList.size();
            }
            insertNum = insertNum + printRecordMapper.batchInsertPrintRecord(printRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改打印记录
     *
     * @param printRecord 打印记录
     * @return 结果
     */
    @Override
    public int updatePrintRecord(PrintRecord printRecord) {
        printRecord.setUpdateBy(SecurityUtils.getUsername());
        printRecord.setUpdateTime(DateUtils.getNowDate());
        return printRecordMapper.updatePrintRecord(printRecord);
    }

    /**
     * 批量修改打印记录
     *
     * @param printRecordList 打印记录列表
     * @return 结果
     */
    @Override
    public int batchUpdatePrintRecord(List<PrintRecord> printRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (PrintRecord printRecord : printRecordList){
            printRecord.setUpdateBy(username);
            printRecord.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<printRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > printRecordList.size()) {
                endIndex = printRecordList.size();
            }
            updateNum = updateNum + printRecordMapper.batchUpdatePrintRecord(printRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除打印记录
     *
     * @param ids 需要删除的打印记录主键集合
     * @return 结果
     */
    @Override
    public int deletePrintRecordByIds(Long[] ids) {
        return printRecordMapper.deletePrintRecordByIds(ids);
    }

    /**
     * 删除打印记录信息
     *
     * @param id 打印记录主键
     * @return 结果
     */
    @Override
    public int deletePrintRecordById(Long id) {
        return printRecordMapper.deletePrintRecordById(id);
    }

    @Override
    public AjaxResult getPrintRecord(String trayNo) {
        if (StringUtils.isEmpty(trayNo) || StringUtils.startsWith(trayNo, "T")) {
            return AjaxResult.error("托盘编号不正确");
        }
        PrintRecord record = printRecordMapper.getPrintRecord(trayNo);
        if (record == null) {
            return AjaxResult.error("没有查询到打印记录");
        }
        return AjaxResult.success("查询成功", record);
    }
}
